package com.quanshu.shop.sort.view;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.sort.adapter.SortTypeLeftAdapter;
import com.quanshu.shop.sort.entity.SortTypeEntity;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsTypeActivity extends BaseActivity {

    @BindView(R.id.goods_type_title)
    BaseTitleBar goodsTypeTitle;
    @BindView(R.id.goods_type_rlv1)
    RecyclerView goodsTypeRlv1;
    private SortTypeLeftAdapter sortTypeLeftAdapter;
    private HashMap<Integer, SortItemFragment> sortItemFragmentHashMap;
    private List<SortTypeEntity.DataEntity> dataEntityList;
    private int selectIndex = 0;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_type);
        ButterKnife.bind(this);
        ImmersionBar.with(this).titleBar(goodsTypeTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        fragmentManager = getSupportFragmentManager();
        sortItemFragmentHashMap = new HashMap<>();
        goodsTypeTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("分类").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        goodsTypeRlv1.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        NetClient.getInstance().getShopService(ShopMainIF.class).getSortTypeInfo("")
                .compose(new BaseTransformer<>(this)).subscribe(new BaseObserver<BaseResponse<List<SortTypeEntity.DataEntity>>>() {
            @Override
            public void onSuccess(BaseResponse<List<SortTypeEntity.DataEntity>> response) {
                dataEntityList = response.getData();
                if (dataEntityList != null && dataEntityList.size() > 0) {
                    dataEntityList.get(0).isSetect = true;
                    SortItemFragment sortItemFragment = SortItemFragment.newInstance("", "", dataEntityList.get(0));
                    sortItemFragmentHashMap.put(0, sortItemFragment);
                    fragmentManager.beginTransaction().add(R.id.goods_type_fml1,sortItemFragment,String.valueOf(0)).show(sortItemFragment).commit();

                }
                sortTypeLeftAdapter = new SortTypeLeftAdapter(response.getData());
                sortTypeLeftAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                        switch (view.getId()) {
                            case R.id.item_sort_type_left_bg:
                                if (position != selectIndex) {
                                    dataEntityList.get(selectIndex).setSetect(false);
                                    dataEntityList.get(position).setSetect(true);
                                    fragmentManager.beginTransaction().hide(sortItemFragmentHashMap.get(selectIndex)).commit();
                                    if (sortItemFragmentHashMap.get(position)!=null){
                                        fragmentManager.beginTransaction().show(sortItemFragmentHashMap.get(position)).commit();
                                    }else {
                                        SortItemFragment sortItemFragment = SortItemFragment.newInstance("", "", dataEntityList.get(position));
                                        sortItemFragmentHashMap.put(position, sortItemFragment);
                                        fragmentManager.beginTransaction().add(R.id.goods_type_fml1,sortItemFragment,String.valueOf(position)).show(sortItemFragment).commit();
                                    }
                                    selectIndex = position;
                                }
                                sortTypeLeftAdapter.notifyDataSetChanged();
                                break;
                        }
                    }
                });
                goodsTypeRlv1.setAdapter(sortTypeLeftAdapter);
            }
        });
    }
}
